﻿#region UsingRegion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using game_for_test_helper.src.graphics;
using game_for_test_helper.src.helper;
using Microsoft.Xna.Framework;
#endregion
namespace game_for_test_helper.src.modelcontrol
{
    class Human : ICameraFollowObject
    {
        private ModelCreator model;
        private Vector3 Position;

        public float PositionY
        {
            get { return Position.Y; }
            set { Position.Y = value; }
        }
        private Vector3 Face;
        private float WalkSpeed;
        private List<string> ActionList;
        private HumanActionState actionState, currentState;
        private static readonly string[] idleStand = new string[] { "Stand", "Stand 2", "Stand 3" };
        public Human()
        {
            model = new ModelCreator("models/HumanMaleNoble");
            ActionList = new List<string>();
            Position = new Vector3(0.0f, 270.0f, 0.0f);
            Face = Vector3.Right;
            WalkSpeed = 0.0f;
            actionState = HumanActionState.Idle;
            currentState = HumanActionState.Idle;
            ra = new Random();
            ActionIdling = false;
            PrePosition = Position;
            dropDownSpeed = 2.0f;
        }
        public void InitPos()
        {
            model.modelInit();
        }
        public void Walk()
        {
            actionState = HumanActionState.Walk;
            WalkSpeed = 4.0f;
        }
        public void WalkDirection(float y, float x)
        {
            Face.Z = y;
            Face.X = x;
            actionState = HumanActionState.Walk;
            WalkSpeed = 4.0f;
        }
        public void WalkFastDirection(float y, float x)
        {
            Face.Z = y;
            Face.X = x;
            actionState = HumanActionState.WalkFast;
            WalkSpeed = 8.0f;
        }
        public void WalkFast()
        {
            actionState = HumanActionState.WalkFast;
            WalkSpeed = 8.0f;
        }
        public void TurnRight()
        {
            Face = Vector3.Transform(Face, Matrix.CreateRotationY(MathHelper.ToRadians(5.0f)));
        }
        public void TurnLeft()
        {
            Face = Vector3.Transform(Face, Matrix.CreateRotationY(MathHelper.ToRadians(-5.0f)));
        }
        private TimeSpan ActionUpdateTime;
        private Vector3 PrePosition;
        public void Update(GameTime gameTime)
        {
            if (Face.Y != 0.0f)
                System.Console.WriteLine("warning");
            PointManger.DrawPoinStatict(Position);
            //TextWriteHelper.StaticWrite(new Vector2(20, 140), Position.ToString());
            #region CheckAndUpdateAction

            if (currentState != actionState)
            {
                currentState = actionState;
                ActionUpdateTime = gameTime.TotalGameTime;
                switch (currentState)
                {
                    case HumanActionState.Idle:
                        model.doAction("Stand", false);
                        ActionIdling = true;
                        break;
                    case HumanActionState.Walk:
                        model.doAction("Walk", false);
                        break;
                    case HumanActionState.WalkFast:
                        model.doAction("Walk Fast", false);
                        break;
                    default:
                        break;
                }
            }
            else
            {
                switch (currentState)
                {
                    case HumanActionState.Idle:
                        if (ActionIdling == false)
                        {
                            int x = ra.Next(100);
                            if (x < 3)
                            {
                                model.doAction(idleStand[x], false);
                                ActionIdling = true;
                                ActionUpdateTime = gameTime.TotalGameTime;
                            }
                        }
                        else
                        {
                            if (gameTime.TotalGameTime.TotalMilliseconds - ActionUpdateTime.TotalMilliseconds >= (model.PlayingCurrentClip1.Duration.TotalMilliseconds * 2))
                            {
                                ActionIdling = false;
                            }
                        }
                        break;
                    case HumanActionState.Walk:
                        Position = Position + Face * ((float)(gameTime.ElapsedGameTime.TotalMilliseconds) / 20.0f) * WalkSpeed;
                        if (gameTime.TotalGameTime.TotalMilliseconds - ActionUpdateTime.TotalMilliseconds >= model.PlayingCurrentClip1.Duration.TotalMilliseconds)
                        {
                            ActionUpdateTime = gameTime.TotalGameTime;
                            actionState = HumanActionState.Idle;
                            model.doAction("Walk", false);
                        }
                        break;
                    case HumanActionState.WalkFast:
                        Position = Position + Face * ((float)(gameTime.ElapsedGameTime.TotalMilliseconds) / 20.0f) * WalkSpeed;
                        if (gameTime.TotalGameTime.TotalMilliseconds - ActionUpdateTime.TotalMilliseconds >= model.PlayingCurrentClip1.Duration.TotalMilliseconds)
                        {
                            ActionUpdateTime = gameTime.TotalGameTime;
                            actionState = HumanActionState.Idle;
                            model.doAction("Walk Fast", false);
                        }
                        break;
                    default:
                        break;
                }
            }
            #endregion
            #region CheckCollision
            if (model.WheatherCollisionScene(Position))
            {
                if (currentState == HumanActionState.Walk || currentState == HumanActionState.WalkFast)
                {
                    float upsize = ((float)(gameTime.ElapsedGameTime.TotalMilliseconds) / 20.0f) * WalkSpeed* 0.5f;
                    /*textwritehelper.staticwrite(new Vector2(60.0f,240f), upsize.tostring());*/
                    Position.Y = PositionY + upsize;
                    if (model.WheatherCollisionScene(Position) == false)
                    {

                    }
                    else
                    {
                        TextWriteHelper.StaticWrite(new Vector2(70, 20), "collision");
                        Position = PrePosition;
                        Collision1 = true;
                    }
                }
                else
                {
                    TextWriteHelper.StaticWrite(new Vector2(70, 20), "collision");
                    Position = PrePosition;
                    Collision1 = true;
                }
            }
            else
            {
                TextWriteHelper.StaticWrite(new Vector2(70, 20), "notcollision");
                Collision1 = false;
            }

            PrePosition = Position;
            //  TextWriteHelper
            Vector3 PositionDown = Position;
            PositionDown.Y -= dropDownSpeed;
            if (model.WheatherCollisionScene(PositionDown) == false)
            {
                Position = PositionDown;
                PositionDown.Y -= dropDownSpeed;
                dropDownSpeed += 0.5f;
                if (dropDownSpeed > 5.0f)
                {
                    dropDownSpeed = 5.0f;
                }
            }
            else
                dropDownSpeed = 2.0f;
            #endregion

            model.update(gameTime);
            return;
        }
        private float dropDownSpeed = 1.0f;
        public void Draw(GameTime gameTime)
        {
            float radias = (float)Math.Atan2(-Face.Z, Face.X);
            model.Render(Matrix.CreateRotationY(radias) * Matrix.CreateTranslation(Position));
        }
        private bool ActionIdling;
        private bool Collision;

        public bool Collision1
        {
            get { return Collision; }
            set { Collision = value; }
        }
        private Random ra;





        #region ICameraFollowObject 成员

        public Vector3 TargetPosition
        {
            get { return Position; }
        }

        #endregion

        #region ICameraFollowObject 成员


        public Vector2 TargetDirection
        {
            get { return new Vector2(Face.X, Face.Z); }
        }

        #endregion
    }

    #region HumanActionState
    public enum HumanActionState
    {
        Idle,
        Attack,
        Stand,
        Stand2,
        Stand3,
        Walk,
        WalkFast
    }
  #endregion
}
